<template>
    <uni-popup-dialog mode="input" :title="userStore.userInfo.password?'修改密码':'设置密码'"
                      @confirm="handle">
        <view v-if="userStore.userInfo.password" class="flex flex-sub flex-direction">
            <view class="margin-xs">
                <uni-easyinput type="password" v-model="oldPassword" placeholder="请输入旧密码"></uni-easyinput>
            </view>
            <view class="margin-xs">
                <uni-easyinput type="password" v-model="newPassword" placeholder="请输入新密码"></uni-easyinput>
            </view>
            <view class="margin-xs">
                <uni-easyinput type="password" v-model="newPasswordAgain"
                               placeholder="请输入再次输入密码"></uni-easyinput>
            </view>
        </view>
        <view v-else class="flex flex-sub flex-direction">
            <view class="flex flex-sub align-center justify-between margin-xs">
                <view class="text-gray ">{{ userStore.userInfo.email }}</view>
                <button :disabled="!isSend" class="cu-btn bg-blue margin-left-xs" @click="getCode">
                    {{ isSend ? '验证码' : second }}
                </button>
            </view>
            <view class="margin-xs">
                <uni-easyinput v-model="code" placeholder="请输入验证码"></uni-easyinput>
            </view>
            <view class="margin-xs">
                <uni-easyinput type="password" v-model="newPassword" placeholder="请输入密码"></uni-easyinput>
            </view>
        </view>
    </uni-popup-dialog>
</template>

<script setup lang="ts">
import {ref} from "vue";
import api from "@/api";
import {useUserStore} from "@/store";
import {encrypt} from "@/utils";

const userStore = useUserStore()
const code = ref('')
const newPassword = ref('')
const newPasswordAgain = ref('')
const oldPassword = ref('')
const isSend = ref(true)
const second = ref(0)


const getCode = () => {
    isSend.value = false
    second.value = 60
    setInterval(() => {
        second.value--
        if (second.value === 1) {
            isSend.value = true
        }
    }, 1000)
    api.mail.code({
        email: userStore.userInfo.email
    }).then(res => {
        if (res && res.data.code !== 0) {
            isSend.value = true
        }
    })
}

const handle = () => {
    if (userStore.userInfo.password) {
        api.user.updatePassword({
            oldPassword: encrypt(oldPassword.value),
            newPassword: encrypt(newPassword.value)
        }).then(res => {
            if (res && res.data.code === 0) {
                uni.showToast({
                    title: res.data.msg
                })
            }
        })
    } else {
        api.user.setPassword({
            email: userStore.userInfo.email,
            code: code.value,
            password: encrypt(newPassword.value)
        }).then(res => {
            if (res && res.data.code === 0) {
                userStore.userInfo.password='***'
                uni.showToast({
                    title: res.data.msg
                })
            }
        })
    }
}
</script>

<style scoped>

</style>